
const exec  =  require('child_process').exec
const  getAllApkInPath  = require('../getAllFile')
const send  = require('koa-send')
//共享文件路径
const sharePath  = "/Users/wt/Documents/file"


//脚本命令函数
function execute(cmd){

    return new Promise((resolve ,reject)=>{
        exec(cmd,function(error,stdout,stderr){
            if (error){
                  reject(error)    
            }else {
    
                resolve("成功")
            }
        })  
    })
}
//打包
var fn_pack = async(ctx,next)=>{
        console.log("接收到请求");
        const osType  = ctx.params.name;
        var cmd = "";
        if (osType == "android"){
          cmd = `cd ${__dirname}/sh  &&  ./pack.sh`
        }else{
            cmd = `cd ${__dirname}/sh  &&  ./pack_ios.sh`
        }
        // 切换到当前目录，并执行脚本 提示：如果脚本提示没有权限的话执行命令： chmod +x ./name.sh  
       await execute(cmd).then((data)=>{

            ctx.restSucess({info:"打包成功"},"请求成功")
        }).catch(error=>{
             ctx.restFailure(error,"请求失败")
        })
}


//查询所有文件
var fn_get_allFile = async(ctx,next)=>{
   var files =  getAllApkInPath(sharePath)
   ctx.restSucess(files,"请求成功")
    
}

//下载
var downFile  = async(ctx,next) =>{
        const fileName  = ctx.params.name;
        console.log(fileName);
        await send(ctx, fileName,{
            root : sharePath
        });

}

module.exports = {
    'GET /api/getUsers/:name': fn_pack,
    'GET /api/getAllFile':fn_get_allFile,
    'GET /api/upload/:name': downFile
}
